/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.ModelState;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.command.LastZoomCommand;

/**
 * A class representing creation dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since July 2004
 */
public final class AttributesSettingsDialog extends SimpleDialog {
	
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
    private JPanel attributesPane = new JPanel();
	
	private JTextField supportColorField = new JTextField();
    private JTextField loadColorField = new JTextField();
    private JTextField distributedLoadColorField = new JTextField();
	private JTextField springColorField = new JTextField();
    private JTextField concForcePointColorField = new JTextField();
    private JTextField displaceColorField = new JTextField();
    private JTextField preStrainColorField = new JTextField();
    private JTextField iniEqForceColorField = new JTextField();
	private JTextField tempChangeColorField = new JTextField();
	private JTextField reactionsColorField = new JTextField();
	
	private JButton chooseSupportColor = new JButton();
    private JButton chooseLoadColor = new JButton();
    private JButton chooseDistributedLoadColor = new JButton();
    private JButton chooseSpringColor = new JButton();
    private JButton chooseConcForcePointColor = new JButton();
    private JButton chooseDisplaceColor = new JButton();
    private JButton choosePreStrainColor = new JButton();
    private JButton chooseIniEqForceColor = new JButton();
	private JButton chooseTempChangeColor = new JButton();
	private JButton chooseReactionsColor = new JButton();
	
    private ModelState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.Interface
     */
    public AttributesSettingsDialog(InsaneInternalInterface parent, ModelState s) {
        this.parent = parent;
        state = s;
		
		chooseSupportColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseLoadColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseDistributedLoadColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseSpringColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseConcForcePointColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseDisplaceColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		choosePreStrainColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseIniEqForceColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseTempChangeColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		chooseReactionsColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		
		//Sets up the attributes panel
		attributesPane.setLayout(new GridLayout(10,3,2,2));
        attributesPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dASDialog01")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		attributesPane.setMaximumSize(new Dimension(500,300));
        attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog02")));
        supportColorField.setEditable(false);
        supportColorField.setBackground(state.supportColor);
        attributesPane.add(supportColorField);
        attributesPane.add(chooseSupportColor);
        chooseSupportColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                supportColorActionPerformed(e);
            }
        } );
        attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog03")));
        loadColorField.setEditable(false);
        loadColorField.setBackground(state.loadColor);
        attributesPane.add(loadColorField);
        attributesPane.add(chooseLoadColor);
        chooseLoadColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loadColorActionPerformed(e);
            }
        } );
        attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog04")));
        distributedLoadColorField.setEditable(false);
        distributedLoadColorField.setBackground(state.distributedLoadColor);
        attributesPane.add(distributedLoadColorField);
        attributesPane.add(chooseDistributedLoadColor);
        chooseDistributedLoadColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                distributedLoadColorActionPerformed(e);
            }
        } );
        attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog05")));
        springColorField.setEditable(false);
        springColorField.setBackground(state.springColor);
        attributesPane.add(springColorField);
        attributesPane.add(chooseSpringColor);
        chooseSpringColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                springColorActionPerformed(e);
            }
        } );
		attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog06")));
        concForcePointColorField.setEditable(false);
        concForcePointColorField.setBackground(state.concForcePointColor);
        attributesPane.add(concForcePointColorField);
        attributesPane.add(chooseConcForcePointColor);
        chooseConcForcePointColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                concForcePointColorActionPerformed(e);
            }
        } );
		attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog07")));
        displaceColorField.setEditable(false);
        displaceColorField.setBackground(state.displaceColor);
        attributesPane.add(displaceColorField);
        attributesPane.add(chooseDisplaceColor);
        chooseDisplaceColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                displaceColorActionPerformed(e);
            }
        } );
		attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog08")));
        preStrainColorField.setEditable(false);
        preStrainColorField.setBackground(state.preStrainsColor);
        attributesPane.add(preStrainColorField);
        attributesPane.add(choosePreStrainColor);
        choosePreStrainColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                preStrainColorActionPerformed(e);
            }
        } );
		attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog09")));
        iniEqForceColorField.setEditable(false);
        iniEqForceColorField.setBackground(state.iniEqForceColor);
        attributesPane.add(iniEqForceColorField);
        attributesPane.add(chooseIniEqForceColor);
        chooseIniEqForceColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                iniEqForceColorActionPerformed(e);
            }
        } );
		attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog10")));
        tempChangeColorField.setEditable(false);
        tempChangeColorField.setBackground(state.tempChangeColor);
        attributesPane.add(tempChangeColorField);
        attributesPane.add(chooseTempChangeColor);
        chooseTempChangeColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tempChangeColorActionPerformed(e);
            }
        } );
		attributesPane.add(new JLabel(parent.getResourceBundle().getString("dASDialog11")));
        reactionsColorField.setEditable(false);
        reactionsColorField.setBackground(state.reactionsColor);
        attributesPane.add(reactionsColorField);
        attributesPane.add(chooseReactionsColor);
        chooseReactionsColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reactionsColorActionPerformed(e);
            }
        } );
		
		setPane(attributesPane);
		
    }
	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(parent, parent.getResourceBundle().getString("dASDialog12"));
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            state.supportColor = supportColorField.getBackground();
            state.loadColor = loadColorField.getBackground();
            state.distributedLoadColor = distributedLoadColorField.getBackground();
			state.springColor = springColorField.getBackground();
			state.concForcePointColor = concForcePointColorField.getBackground();
			state.displaceColor = displaceColorField.getBackground();
			state.preStrainsColor = preStrainColorField.getBackground();
			state.iniEqForceColor = iniEqForceColorField.getBackground();
			state.tempChangeColor = tempChangeColorField.getBackground();
			state.reactionsColor = reactionsColorField.getBackground();
			
			parent.setModel(parent.getModel());
            this.setVisible(false);
			
			LastZoomCommand cmd = new LastZoomCommand(parent);
			cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
    private void supportColorActionPerformed(ActionEvent e) {
        supportColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog13"), supportColorField.getBackground()));
    }
    
//*****************************************************************************
	
    private void loadColorActionPerformed(ActionEvent e) {
        loadColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog14"), loadColorField.getBackground()));
    }
	
//*****************************************************************************
	
  	private void distributedLoadColorActionPerformed(ActionEvent e) {
        distributedLoadColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog15"), distributedLoadColorField.getBackground()));
    }
    
//*****************************************************************************
	
	private void springColorActionPerformed(ActionEvent e) {
        springColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog16"), springColorField.getBackground()));
    }
    
//*****************************************************************************
	
	private void concForcePointColorActionPerformed(ActionEvent e) {
        concForcePointColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog17"), concForcePointColorField.getBackground()));
    }
    
//*****************************************************************************
	
	private void displaceColorActionPerformed(ActionEvent e) {
        displaceColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog18"), displaceColorField.getBackground()));
    }
    
//*****************************************************************************
	
	private void preStrainColorActionPerformed(ActionEvent e) {
        preStrainColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog19"), preStrainColorField.getBackground()));
    }
    
//*****************************************************************************
	
	private void iniEqForceColorActionPerformed(ActionEvent e) {
        iniEqForceColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog20"), iniEqForceColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void tempChangeColorActionPerformed(ActionEvent e) {
        tempChangeColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog21"), tempChangeColorField.getBackground()));
    }
	
//*****************************************************************************
	
	private void reactionsColorActionPerformed(ActionEvent e) {
        reactionsColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dASDialog22"), reactionsColorField.getBackground()));
    }
	
//*****************************************************************************
}